local huoji = fk.CreateSkill {
  name = "3s__huoji",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["3s__huoji"] = "火偈",
  [":3s__huoji"] = "锁定技，一轮内，你首次指定角色或成为一种花色的牌的目标时，你将之改为【火攻】，若造成伤害，使用者视为对目标使用此牌。四种花色均出现后，你分配1点火焰伤害。",
  ["#3s__huoji-target"] = "火偈：选择一名角色分配1点火焰伤害",
  ["@huoji_suit-round"] = "火偈",
  ["@@huoji_damage-round"] = "火偈：已分配",

  ["$3s__huoji1"] = "火由心生，势不可当！",
  ["$3s__huoji2"] = "四象齐聚，耐火焚天！",
}

-- 分配火焰伤害
local function dealFireDamage(player)
  local room = player.room
  -- 选择目标分配1点火焰伤害
  local targets = room:askToChoosePlayers(player, {
    targets = room:getAlivePlayers(),
    min_num = 1,
    max_num = 1,
    skill_name = huoji.name,
    prompt = "火偈：选择一名角色分配1点火焰伤害",
    cancelable = false
  })
  if #targets > 0 then
    room:damage({
      from = player,
      to = targets[1],
      damage = 1,
      damageType = fk.FireDamage,
      skillName = huoji.name
    })
    player.room:addPlayerMark(player, "@@huoji_damage-round")
  end
end

-- 使用牌时检测花色
huoji:addEffect(fk.AfterCardUseDeclared, {
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(huoji.name) and #data:getAllTargets() > 0 then
      local card = data.card
      if card.suit == Card.NoSuit then return end
      return not table.contains(player:getTableMark("@huoji_suit-round"), card:getSuitString(true))
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = data.card
    room:addTableMarkIfNeed(player, "@huoji_suit-round", card:getSuitString(true))
    if #player:getTableMark("@huoji_suit-round") == 4 and player:isAlive() then
      dealFireDamage(player)
    end
    -- 改为火攻结算
    data.extra_data = data.extra_data or {}
    data.extra_data.huoji = data.card.name
    data:changeCard("lvshi__fire_attack", card.suit, card.number, huoji.name)
    --data.extra_data = data.extra_data or {}
    --data.extra_data.huoji = true
    -- 检查是否四种花色都已出现
  end,
})

-- 成为牌的目标时检测花色
huoji:addEffect(fk.TargetConfirmed, {
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(huoji.name) then
      local card = data.card
      if card.suit == Card.NoSuit then return end
      return not table.contains(player:getTableMark("@huoji_suit-round"), card:getSuitString(true))
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = data.card
    room:addTableMarkIfNeed(player, "@huoji_suit-round", card:getSuitString(true))
    if #player:getTableMark("@huoji_suit-round") == 4 and player:isAlive() then
      dealFireDamage(player)
    end
    -- 改为火攻结算
    data.extra_data = data.extra_data or {}
    data.extra_data.huoji = data.card.name
    data:changeCard("lvshi__fire_attack", card.suit, card.number, huoji.name)
    --data.extra_data = data.extra_data or {}
    --data.extra_data.huoji = true
    -- 检查是否四种花色都已出现
  end,
})

huoji:addEffect(fk.CardUseFinished, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(huoji.name, true, true) and data.extra_data and data.extra_data.huoji
    and data.from:isAlive() and data.damageDealt
  end,
  on_use = function(self, event, target, player, data)
    local name = data.extra_data.huoji
    player.room:useVirtualCard(name, nil, data.from, data:getAllTargets(), huoji.name)
  end,
})

return huoji